import { defineStore } from "pinia";
import { IUser, IUserInfo } from "../../type/User";
export const useUserStore = defineStore({
  id: 'user',
  state: (): IUser & IUserInfo => ({
    username: '',
    password: '',
    token: '',
    roles: [],
  }),
  getters: {
    getToken: (state) => {
      return JSON.parse(localStorage.getItem('userInfo') || '{}').token || '';
    },
    getRoles: (state) => {
      return state.roles || JSON.parse(localStorage.getItem('userInfo') || '{}').roles || [];
    }
  },
  actions: {
    setUserInfo(params: IUser & IUserInfo) {
      this.username = params.username;
      this.password = params.password;
      this.token = params.token;
      this.roles = params.roles;
      localStorage.setItem('userInfo', JSON.stringify(params));
    },
    removeUserInfo() {
      this.username = '';
      this.password = '';
      this.token = '';
      this.roles = [];
      localStorage.removeItem('userInfo');
    }
  }
})